import wepy from 'wepy'

export default class extends wepy.mixin {
    data = {
        // 轮播图
        swipeList: [],
        //播报文章
        article: [],
        //分类
        cate: [],
        //大广告
        bigAd: {},
        //民宿推荐
        homestay: []
    }

    onLoad() {
        this.getSwiperData()
        this.getArticle()
        this.getCate()
        this.getAd()
        this.getHomestay()
    }

    //获取轮播图
    async getSwiperData() {
        const { data: res } = await wepy.request({
            url: 'http://minsu.xiaoxingzi028.com/',
            method: 'POST'
        })
        console.log(res)
        if (res.msg !== 'OK') {
            wepy.showToast({
                title: '获取轮播图失败',
                icon: 'none',
                duration: 2000
            })
            return
        }

        this.swipeList = res.data.ad
        this.$apply()
        // console.log(this.swipeList)
    }

    //获取播报
    async getArticle() {
        const { data: res } = await wepy.request({
            url: 'http://minsu.xiaoxingzi028.com/',
            method: 'POST'
        })
        if (res.msg !== 'OK') {
            wepy.showToast({
                title: '获取播报信息失败',
                icon: 'none',
                duration: 2000
            })
            return
        }
        this.article = res.data.article
        this.$apply()
    }

    //获取分类
    async getCate() {
        const { data: res } = await wepy.request({
            url: 'http://minsu.xiaoxingzi028.com/',
            method: 'POST'
        })
        if (res.msg !== 'OK') {
            wepy.showToast({
                title: '获取播报信息失败',
                icon: 'none',
                duration: 2000
            })
            return
        }
        this.cate = res.data.homestay
        this.$apply()
        // console.log(this.cate)
    }

    //获取大广告
    async getAd() {
        const { data: res } = await wepy.request({
            url: 'http://minsu.xiaoxingzi028.com/',
            method: 'POST'
        })
        if (res.msg !== 'OK') {
            wepy.showToast({
                title: '获取播报信息失败',
                icon: 'none',
                duration: 2000
            })
            return
        }
        this.bigAd = res.data.ad2
        this.$apply()
        console.log(this.bigAd)
    }

    //获取民宿
    async getHomestay() {
        const { data: res } = await wepy.request({
            url: 'http://minsu.xiaoxingzi028.com/',
            method: 'POST'
        })
        if (res.msg !== 'OK') {
            wepy.showToast({
                title: '获取民宿信息失败',
                icon: 'none',
                duration: 2000
            })
            return
        }
        this.homestay = res.data.homestay
        this.$apply()
        console.log(this.homestay)
    }

    methods = {
        // 查看更多民宿
        viewMore() {
            console.log('更多民宿');
        },
        //去民宿预定页面
        toHome() {
            console.log(111);
            wepy.navigateTo({
                url: '/pages/reservationHome',
                events: {
                    // 为指定事件添加一个监听器，获取被打开页面传送到当前页面的数据
                    // acceptDataFromOpenedPage: function (data) {
                    //     console.log(data)
                    // },
                    // someEvent: function (data) {
                    //     console.log(data)
                    // }
                },
                success: function (res) {
                    // 通过eventChannel向被打开页面传送数据
                    // res.eventChannel.emit('acceptDataFromOpenerPage', { data: 'test' })
                }
            })
        }
    }
}